<?php
namespace App\Http\Controllers;

use App\Http\Requests\CreateRoleRequest;
use App\Http\Requests\UpdateRoleRequest;
use App\Repositories\PermissionRepository;
use App\Repositories\RoleRepository;
use Toastr;

class RoleController extends Controller
{

    protected $role, $permission;
    protected $fields = [
        'name' => '',
        'display_name' => '',
        'description' => '',
        'perms' => [],
    ];

    public function __construct(RoleRepository $role, PermissionRepository $permission)
    {
        parent::__construct();
        $this->role = $role;
        $this->permission = $permission;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $roles = $this->role->paginate();
        return view('role.index', compact('roles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        $data['permissions'] = buildPermission($this->permission->getAll());
        return view('role.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \App\Http\Requests\CreateRoleRequest $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(CreateRoleRequest $request)
    {
        $data = $request->all();
        if ($role = $this->role->create($data)) {
            $role->attachPermissions($data['permission']);
            Toastr::success('添加成功');
            return redirect('/role');
        }
        Toastr::error('添加失败');
        return redirect('/role');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = $this->role->find($id);
        $permissions = [];
        if ($role->perms) {
            foreach ($role->perms as $v) {
                $permissions[] = $v->id;
            }
        }
        $role->perms = $permissions;
        foreach (array_keys($this->fields) as $field) {
            $data[$field] = old($field, $role->$field);
        }
        $data['permissions'] = buildPermission($this->permission->getAll());
        $data['id'] = $id;
        return view('role.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \App\Http\Requests\UpdateRoleRequest $request
     * @param int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRoleRequest $request, $id)
    {
        $data = $request->all();
        if ($role = $this->role->update($data, $id)) {
            isset($data['permission']) ? $role->perms()->sync($data['permission']) : $role->perms()->sync([]);
            Toastr::success('更新成功');
            return redirect('/role');
        }
        Toastr::error('更新失败');
        return redirect('/role');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if ($this->role->delete($id)) {
            Toastr::success('删除成功');
            return redirect('/role');
        } else {
            Toastr::error('删除失败');
            return redirect('/role');
        }

    }
}
